package com.ln.lnzw.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.ln.lnzw.R;
import com.ln.lnzw.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wuwei on 2018/4/12.
 */

public class UserAgreementActivity extends BaseActivity  {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.wv_webview)
    WebView wvWebview;
    @BindView(R.id.tv_name)
    TextView tvName;

    private String webUrl;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_agreement);
        ButterKnife.bind(this);
        webUrl = getIntent().getStringExtra("webUrl");
        Log.i("000", "onCreate: "+webUrl);
        name = getIntent().getStringExtra("name");
        tvName.setText(name);

        wvWebview.getSettings().setSupportZoom(true);//支持缩放
        wvWebview.getSettings().setBuiltInZoomControls(true);
        wvWebview.getSettings().setUseWideViewPort(true);
        wvWebview.getSettings().setDisplayZoomControls(false);
        wvWebview.getSettings().setLoadWithOverviewMode(true);
        wvWebview.getSettings().setJavaScriptEnabled(true);
        wvWebview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wvWebview.loadUrl(webUrl);
        wvWebview.setWebViewClient(new WebViewClient());
        wvWebview.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Uri uri = Uri.parse(url);

                Intent intent = new Intent(Intent.ACTION_VIEW, uri);

                startActivity(intent);
            }
        });

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                if (wvWebview.canGoBack()) {
                    wvWebview.goBack();
                }
                else{
                    finish();
                }
                break;

        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wvWebview.canGoBack()) {
            wvWebview.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
